package com.example.ecommerce.controller;

import com.example.ecommerce.entity.Product;
import com.example.ecommerce.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/products")
    public List<Product> getProducts(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size,
            @RequestParam(required = false) String search) {

        return productService.getProducts(page, size, search);
    }

    @GetMapping("/my-products")
    public List<Product> getMyProducts(@RequestParam Long uid) {
        return productService.getProductsByUid(uid);
    }

    // 修改商品信息（名称、价格、描述、图片）
    @PostMapping("/update-product")
    public boolean updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    // 删除商品（下架）
    @DeleteMapping("/delete-product/{id}")
    public boolean deleteProduct(@PathVariable Long id) {
        return productService.deleteProductById(id);
    }
    @PostMapping("/add-product")
    public boolean addProduct(@RequestBody Product product){
        return productService.addProduct(product);
    }


}
